package tk.uodrad.apex.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.uodrad.apex.project.ex.ServiceException;
import tk.uodrad.apex.project.pojo.dto.ActivityAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.ActivityUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.ActivityManagement;
import tk.uodrad.apex.project.pojo.entity.Building;
import tk.uodrad.apex.project.pojo.entity.People;
import tk.uodrad.apex.project.pojo.vo.ActivityDashboardVO;
import tk.uodrad.apex.project.pojo.vo.ActivityListVO;
import tk.uodrad.apex.project.service.ActivityManagementService;
import tk.uodrad.apex.project.mapper.ActivityManagementMapper;
import org.springframework.stereotype.Service;
import tk.uodrad.apex.project.utils.InitRandomCode;
import tk.uodrad.apex.project.web.ServiceCode;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【activity_management】的数据库操作Service实现
* @createDate 2023-04-08 15:42:25
*/
@Service
@Slf4j
public class ActivityManagementServiceImpl extends ServiceImpl<ActivityManagementMapper, ActivityManagement>
    implements ActivityManagementService{

    @Autowired
    private ActivityManagementMapper activityMapper;

    @Override
    public void setEnable(Long id) {
        log.debug("开始处理【启用活动】的业务，参数：{}", id);
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Long id) {
        log.debug("开始处理【禁用活动】的业务，参数：{}", id);
        updateEnableById(id, 0);
    }

    private void updateEnableById(Long id, Integer state) {
        // 调用Mapper对象的selectById()方法执行查询
        ActivityManagement currentActivity = activityMapper.selectById(id);
        // 判断查询结果是否为null，如果是，则抛出异常
        if (currentActivity == null) {
            String message = ENABLE_TEXT[state] + "活动失败，活动数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable与参数enable是否相同，如果是，则抛出异常（当前状态与目标状态相同，没必要执行更新）
        if (currentActivity.getState().equals(state)) {
            String message = ENABLE_TEXT[state] + "活动失败，此活动已经处于" + ENABLE_TEXT[state] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建ActivityManagement对象
        ActivityManagement updateActivity = new ActivityManagement();
        // 向Category对象中封装属性值：id, enable，均来自方法参数
        updateActivity.setState(state);
        //创建MyBatis-plus提供的Wrappers类下的query()方法
        QueryWrapper<ActivityManagement> wrapper = Wrappers.query();
        //用eq方法用于判断数据库中是否有id等于当前ActivityManagement的id（其他模块一般根据实际情况判断数据是否存在）
        wrapper.eq("id", id);
        // 调用Mapper对象的update()方法执行更新
        int rows = activityMapper.update(updateActivity, wrapper);
        if (rows != 1) {
            String message = ENABLE_TEXT[state] + "活动失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
    
    @Override
    public void save(ActivityAddNewDTO activityAddNewDTO) {
        log.debug("开始处理【添加活动】的业务，参数：{}", activityAddNewDTO);

        // 调用参数对象的getName()得到尝试添加的活动的名称
        String name = activityAddNewDTO.getTitle();
        //创建MyBatis-plus提供的Wrappers类下的query()方法
        QueryWrapper<ActivityManagement> wrapper = Wrappers.query();
        //用eq方法用于判断数据库中是否有name等于当前Activity的title
        wrapper.eq("title", name);
        ActivityManagement selectOne = activityMapper.selectOne(wrapper);
        log.debug("根据名称统计数量，结果：{}", selectOne);
        // 判断统计结果是否不为null
        if (selectOne != null) {
            // 是：名称被占用，抛出异常
            String message = "添加活动失败，活动名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建ActivityManagement对象
        ActivityManagement activity = new ActivityManagement();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到Album对象中
        BeanUtils.copyProperties(activityAddNewDTO, activity);
        //给people对象赋值创建时间
        activity.setGmtCreate(new Date());
        // 调用Mapper对象的insert()执行插入数据
        log.debug("即将执行插入数据，参数：{}", activity);
        int rows = activityMapper.insert(activity);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void updateById(ActivityUpdateDTO activityUpdateDTO) {
        // 获取要修改的数据的id
        Long id = activityUpdateDTO.getId();
        log.debug("开始处理【修改活动】的业务，ID：{}，新数据：{}", id, activityUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        ActivityManagement ActivityById = activityMapper.selectById(id);
        // 判断查询结果是否为null
        if (ActivityById == null) {
            // 是：抛出异常
            String message = "修改活动失败，尝试修改的活动数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 创建ActivityManagement对象
        ActivityManagement activity = new ActivityManagement();
        // 复制属性
        BeanUtils.copyProperties(activityUpdateDTO, activity);
        //给people对象赋值创建时间
        activity.setGmtModified(new Date());
        // 调用Mapper对象的update()方法执行修改
        int rows = activityMapper.updateById(activity);
        if (rows != 1) {
            String message = "服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public IPage<ActivityListVO> list(Integer pageNum, Integer pageSize, String communityCode) {
        log.info("进入service, pageNum:{}, pageSize:{}, communityCode:{}", pageNum, pageSize, communityCode);
        Page<ActivityManagement> page = new Page<>(pageNum, pageSize);
        return activityMapper.selectPageVO(page, communityCode);
    }

    @Override
    public List<ActivityDashboardVO> listDashboard() {
        return activityMapper.selectDashboard();
    }
    
    
    
    
}




